package com.lsk.applet;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.List;

public class DealingCouandTic implements Runnable{

    @Override
    public void run() {
        System.out.print("优惠券，商品定时任务执行了");
        //查询过期的会员优惠券
        String sql="SELECT a.id as aid  FROM yx_coupon_card a JOIN yx_coupon b ON a.couponId=b.id WHERE a.state='0' AND b.endTime<now() GROUP BY a.id";
        try {
            List<Record> yx_coupon_cardList= Db.find(sql);
            List<String> updateList=new ArrayList<>();
            List<String> updateList2=new ArrayList<>();

            if(yx_coupon_cardList.size()>0){
                for(int i=0;i<yx_coupon_cardList.size();i++){
                    String updateSql="UPDATE yx_coupon_card SET state=3 WHERE id='"+yx_coupon_cardList.get(i).get("aid")+"'";

                    updateList.add(updateSql);
                }
                Db.batch(updateList,updateList.size());  //批量修改会员优惠卷的状态

            }

            //查询过期的优惠券
            String sql2="SELECT a.id FROM yx_coupon a where a.endTime<NOW() and a.state=0";

            List<Record> yx_couponList= Db.find(sql2);
            if(yx_couponList.size()>0) {
                for (int a=0;a<yx_couponList.size();a++) {
                    String updateSql="UPDATE yx_coupon SET state=1 WHERE id='"+yx_couponList.get(a).get("id")+"'";
                    updateList2.add(updateSql);

                }
                Db.batch(updateList2,updateList2.size());  //批量修改优惠卷的状态

            }




            //查询过期的商品
            String ticketSql="SELECT * FROM ticket WHERE snapTime<=NOW() and state!=2";

            List<Record> ticketList=Db.find(ticketSql);
            List<String> updateTicketList=new ArrayList<>();
            if(ticketList.size()>0){
                for(int m=0;m<ticketList.size();m++){
                    String updateSql="UPDATE ticket SET state=2 WHERE id='"+ticketList.get(m).get("id")+"'";
                    updateTicketList.add(updateSql);
                }
                Db.batch(updateTicketList,updateTicketList.size());//批量修改商品状态
            }





        }catch (Exception e){

            e.printStackTrace();
        }
    }
}
